<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :image-list="goods.mainPictures" />
          <GoodsSale :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsInfo :goods="goods" />
          <!-- 规格选择组件 -->
          <XtxGoodSku :goods="goods" @change="goodSkuChangeFn"  />
          <!-- 数量选择组件 -->
          <XtxNumbox v-model="count" :min="1" :max="goods.inventory" />
          <!-- 按钮组件 -->
          <XtxButton @click="addToCart()" type="primary" style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goods" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
          <GoodsHot :goodsId="goods.id" :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoodsDetail } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
import GoodsSale from './components/goods-sale.vue'
import GoodsInfo from './components/goods-info.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { useStore } from 'vuex'
import { message } from '@/components/Message'

export default {
  name: 'XtxGoodsPage',
  components: { GoodsSale, GoodsInfo, GoodsDetail, GoodsHot },
  setup () {
    const route = useRoute()
    const goods = ref(null)
    findGoodsDetail(route.params.id).then(({ result }) => {
      console.log(result)
      goods.value = result
    })

    const goodSkuChangeFn = (val) => {
      // console.log('商品详情', goods.value)
      console.log('规格选择组件子传父结果', val)
      // 如果选择了规格，才更新商品信息
      if (val.skuId) {
        // goods.value.price = val.price
        // goods.value.oldPrice = val.oldPrice
        // goods.value.inventory = val.inventory
        // goods.value.skuId = val.skuId
        // goods.value.specsText = val.specsText
        // 🔔 通过遍历对象化简代码
        for (const key in val) {
          goods.value[key] = val[key]
        }
      }
    }

    // 商品数量
    const count = ref(1)

    const store = useStore()
    // 加入购物车按钮
    const addToCart = () => {
      // 1. 没有sku规格数据
      if (!goods.value.skuId) return message({ text: '请选择商品规格' })
      // 2. 添加的不能超过库存
      if (count.value > goods.value.inventory) return message({ text: '库存超出了' })
      // 3. 收集商品信息 - 💥 注意：后端接口字段命名不规范，要注意字段名匹配
      const goodsObj = {
        id: goods.value.id, // 商品id
        name: goods.value.name, // 商品名称
        picture: goods.value.mainPictures[0], // 商品图片
        skuId: goods.value.skuId, // 有效skuId
        price: goods.value.oldPrice, // 原价格
        nowPrice: goods.value.price * 10, // 现价格 - 💥人为制造错误的价格，用于测试商品价格更新
        attrsText: goods.value.specsText, // 规格文案
        stock: goods.value.inventory, // 库存
        isEffective: true, // 是否有效商品
        selected: true, // 是否选中
        count: count.value // 商品数量
      }
      console.log('后端返回的', goods.value)
      console.log('加入购物城用的', goodsObj)
      // 调用 action
      store.dispatch('cart/asyncInertCart', goodsObj).then(() => {
        // 添加成功提示
        message({ type: 'success', text: '添加购物车成功' })
        goods.value.inventory -= count.value
      })
    }

    return { goods, goodSkuChangeFn, count, addToCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
